<?php

namespace App\Admin\Controllers\Market;

use App\Enum\CouponEnum;
use App\Models\Coupon;
use App\Models\CouponServiceType;
use App\Models\ServiceType;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;

class CouponController extends AdminController
{

    public $title = '优惠券';

    protected function grid()
    {

        return Grid::make(new Coupon(), function (Grid $grid) {

            $grid->model()->with('service_type');

            $grid->column('name', '优惠券名称');

            $grid->column('service_type', '可用业务')->display(function ($service_type) {
                return implode('，', array_column($service_type, 'name'));
            });
            $grid->column('price_max', '最高折扣(折扣/元)');
            $grid->column('type', '优惠方式')->display(function ($type) {
                return CouponEnum::TypeMessage[$type];
            });

            $grid->column('coupon_value', '优惠价值')->display(function ($coupon_value) {
                $unit = $this->type == CouponEnum::TypeProportion ? '折' : '元';
                return $coupon_value . $unit;
            });

            $grid->column('validity_day', '有效期')->display(function ($validity_day) {
                return $validity_day == 0 ? '无期限' : $validity_day . '天';
            });

            $grid->column('use_time', '使用时段')->display(function () {
                return $this->use_time_start . '-' . $this->use_time_end;
            });
            $grid->column('rules', '使用规则');
            $grid->status('状态')->switch();

            $grid->disableViewButton();

            $grid->filter(function (Grid\Filter $filter) {

                $filter->where('service_type', function ($query) {
                    $coupon_service_type = \App\Models\CouponServiceType::query()->where('service_type_id', $this->input)->pluck('coupon_id');
                    $query->whereIn('id', $coupon_service_type->all());
                }, '可用业务')->select(ServiceType::query()->where('status', 1)->orderBy('id')->pluck('name', 'id'));

                $filter->like('name', '优惠券名称');
            });
        });

    }


    protected function form()
    {

        return Form::make(new Coupon(), function (Form $form) {

            $form->text('name', '优惠券名称')->required();

            if ($form->model()->id) {
                $select_service_type = \App\Models\CouponServiceType::query()->where('coupon_id', $form->model()->id)->get()->toArray();
                $form->model()->service_type = implode(',', array_column($select_service_type, 'service_type_id'));
            }
            $form->multipleSelect('service_type', '可用业务')->options(ServiceType::query()->pluck('name', 'id'))->required();

            $form->radio('type', '优惠方式')->options(CouponEnum::TypeMessage)->default(1)->required();
            $form->number('price_max', '最高折扣(折扣/元)')->default(0)->required()->append('0表示不限制');
            $form->number('coupon_value', '优惠幅度(折扣/元)')->min(0)->required();

            $form->number('validity_day', '有效期限(0-无期限)天')->default(0)->min(0)->required();

           // $form->timeRange('use_time_start', 'use_time_end', '使用时段')->default('00:00:00','23:59:59')->required();
            $form->time('use_time_start','开始时间段')->default('00:00:00')->required();
            $form->time('use_time_end','结束时间段')->default('23:59:59')->required();
            $form->number('use_full', '满减条件 元')->min(0)->required();
            $form->text('rules', '使用规则')->required()
                ->append('使用规则内容将展示在乘客端,请根据设置内容如实填写');
            $form->disableViewCheck();
            $form->disableViewButton();

            $form->saved(function ($form) {
                $coupon_service_type_model = new CouponServiceType();
                $coupon_service_type_model->setData((int)$form->getKey(), (array)$form->service_type);
            });

        });

    }


}
